package cn.iocoder.yudao.module.jifenyuan.service.usercurrentroom;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.jifenyuan.controller.app.usercurrentroom.vo.*;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.usercurrentroom.UserCurrentRoomDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.jifenyuan.convert.usercurrentroom.UserCurrentRoomConvert;
import cn.iocoder.yudao.module.jifenyuan.dal.mysql.usercurrentroom.UserCurrentRoomMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jifenyuan.enums.ErrorCodeConstants.*;

/**
 * 查看当前所在房间 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCurrentRoomServiceImpl implements UserCurrentRoomService {

    @Resource
    private UserCurrentRoomMapper userCurrentRoomMapper;

    @Override
    public Long createUserCurrentRoom(AppUserCurrentRoomCreateReqVO createReqVO) {
        // 插入
        UserCurrentRoomDO userCurrentRoom = UserCurrentRoomConvert.INSTANCE.convert(createReqVO);
        userCurrentRoomMapper.insert(userCurrentRoom);
        // 返回
        return userCurrentRoom.getId();
    }

    @Override
    public void updateUserCurrentRoom(AppUserCurrentRoomUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserCurrentRoomExists(updateReqVO.getId());
        // 更新
        UserCurrentRoomDO updateObj = UserCurrentRoomConvert.INSTANCE.convert(updateReqVO);
        userCurrentRoomMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCurrentRoom(Long id) {
        // 校验存在
        validateUserCurrentRoomExists(id);
        // 删除
        userCurrentRoomMapper.deleteById(id);
    }

    private void validateUserCurrentRoomExists(Long id) {
        if (userCurrentRoomMapper.selectById(id) == null) {
            throw exception(USER_CURRENT_ROOM_NOT_EXISTS);
        }
    }

    @Override
    public UserCurrentRoomDO getUserCurrentRoom(Long id) {
        return userCurrentRoomMapper.selectById(id);
    }

    @Override
    public List<UserCurrentRoomDO> getUserCurrentRoomList(Collection<Long> ids) {
        return userCurrentRoomMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserCurrentRoomDO> getUserCurrentRoomPage(AppUserCurrentRoomPageReqVO pageReqVO) {
        return userCurrentRoomMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserCurrentRoomDO> getUserCurrentRoomList(AppUserCurrentRoomExportReqVO exportReqVO) {
        return userCurrentRoomMapper.selectList(exportReqVO);
    }

}
